package com.zlm.custom.mybatis;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

/**
 * @author zlm
 * @date 2025/4/22
 */
public class Main {

    public static void main(String[] args) {
        MySqlSessionFactory factory = new MySqlSessionFactory();
        UserMapper mapper = factory.getMapper(UserMapper.class);
        User user = mapper.selectUserById(1);
        System.out.println(user);
        User ls = mapper.selectUserByName("ls");
        System.out.println(ls);
        System.out.println(mapper.toString());
    }

    private static User jdbcSelectUserById(int id) {
        String jdbcUrl = "jdbc:mysql://localhost:3306/mybatis";
        String username = "root";
        String password = "root";
        String sql = "select id,name,age from t_user where id = ?";
        try(Connection connection = DriverManager.getConnection(jdbcUrl, username, password)) {
            PreparedStatement statement = connection.prepareStatement(sql);
            statement.setInt(1, id);
            ResultSet rs = statement.executeQuery();
            while (rs.next()) {
                User user = new User();
                user.setId(rs.getInt("id"));
                user.setName(rs.getString("name"));
                user.setAge(rs.getInt("age"));
                return user;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
